<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择安装商品"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <contractAzMx ref="contractAzMx"></contractAzMx>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
    
    <script>
import VolBox from "@/components/basic/VolBox.vue";
import contractAzMx from "@/views/aftersaleinfo/contract_az_mx/q_contract_az_mx.vue";
export default {
  components: {
    "vol-box": VolBox,
    contractAzMx,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    onModelClose() {
      this.$refs.contractAzMx.searchFormFields.htid = null;
      this.$refs.contractAzMx.searchFormFields.bm = null;
      this.$refs.contractAzMx.searchFormFields.mc = null;
      this.$refs.contractAzMx.searchFormFields.wsqsl = [null, null];
    },
    async open(ID, sub) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.contractAzMx) {
          clearInterval(setIntervalTiem);
          this.$refs.contractAzMx.searchFormFields.htid = sub;
          this.$refs.contractAzMx.searchFormFields.wsqsl = [1, null];
          this.$refs.contractAzMx.$refs.grid.search();
        }
      }, 100);
    },
    async detailOnSelect() {
      let rowObj = this.$refs.contractAzMx.$refs.grid.getSelectRows();
      if (!rowObj.length) this.base.ElMessageFn("请至少选择一个商品");
      let wsqslSome = rowObj.some((item) => item.wsqsl == 0);
      if (wsqslSome) {
        this.base.ElMessageFn("请选择未安装数量大于零的商品");
        return;
      }
      this.$emit("parentCall", ($parent) => {
        let detailDate = $parent.$refs.detail.rowData;
        let arrID = [];
        detailDate.forEach((item) => {
          let index = rowObj.findIndex((x) => {
            x.ID == item.sales_contractList_ID
              ? arrID.push({ ID: x.ID, goods_name: x.mc })
              : "";
            return x.ID == item.sales_contractList_ID;
          });
          if (index != -1) {
            rowObj.splice(index, 1);
          }
        });
        let strTitle = "";
        arrID.forEach((item) => {
          if (strTitle != "") {
            strTitle += "," + item.goods_name;
          } else {
            strTitle = item.goods_name;
          }
        });
        if (strTitle != "") {
          this.base.ElMessageFn("商品" + strTitle + "已存在安装单明细");
        }
        let arr = rowObj.map((item) => {
          return {
            Goods_sub: item.bm,
            Goods_name: item.mc,
            Goods_num: item.wsqsl,
            Goods_size: item.gg,
            sales_contractList_ID: item.ID,
            Sales_contract_voucher_sub: item.htid,
          };
        });
        $parent.$refs.detail.rowData.unshift(...arr);
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>
    
    <style>
</style>